package com.suneee.permission.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.suneee.core.util.StringUtils;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;
import com.suneee.permission.bean.PermissionDto;
import com.suneee.permission.dao.PermissionDao;
import com.suneee.permission.entity.Permission;

@Repository("permissionDao")
public class PermissionDaoImpl extends GenericDao<Permission, Integer>implements PermissionDao {

	@Override
	public List<Permission> permissionList(PermissionDto dto) 
	{
		WhereClauseBuilder wcb = new WhereClauseBuilder("select * from t_permission where 1 = 1 ");
		wcb.equals("group_name", dto.getGroupName());
		wcb.equals("name", dto.getName());
		wcb.equals("type", dto.getType());
		wcb.equals("code", dto.getCode());
		wcb.equals("app_code",dto.getAppCode());
		if(dto.getEnabled() != null){
			wcb.equals("enabled", dto.getEnabled() ? 1 : 0);
		}
		wcb.between("create_time", dto.getCreateTimeStart(), dto.getCreateTimeEnd());
		wcb.between("last_update_time", dto.getLastUpdateTimeStart(), dto.getLastUpdateTimeEnd());
		return this.executeQuery(wcb.getSql(), wcb.getArgs());
		
	}
	
	@Override
	public List<Permission> permissionNewList(int len,String parentCode,String groupName) 
	{
		
		if(StringUtils.isEmpty(parentCode))
		{
			Object [] args = new Object[2];
			args[0] = len;
			args[1] = groupName;
			return this.selectWhere(" LENGTH(CODE) = ?  and group_name = ? ", args);
		}
		else
		{
			Object [] args = new Object[3];
			args[0] = len;
			args[1] = parentCode + "%";
			args[2] = groupName;
			return this.selectWhere(" LENGTH(CODE) = ? and code like ? and group_name = ? ", args);
		}
	}

	@Override
	public List<Permission> notUsedPermissionList(Integer roleId,String enterpriseCode) {
		String sql = "select * from t_permission where permission_id "
				+ " not in (select permission_id from t_role_permission where role_id = "
				+ roleId + ") ";
		Object[] args = null;
		return this.executeQuery(sql, args);
	}

	@Override
	public List<Permission> publicPermissions(String group) {
		String sql = " permission_group = ? ";
		return this.selectWhere(sql, group);
	}

	@Override
	public boolean isExitsMenuName(Permission permission) 
	{
		List<Map<String,Object>> listResult = null;
		//显示类型(1菜单,2界面按钮,3列表行按钮)
		if(1 == permission.getShowType())
		{
			//判断是否是添加一级菜单 （添加一级菜单不会传递该值）
			if(StringUtils.isEmpty(permission.getParentCode()))
			{
				Object [] args = new Object[3];
				args[0] = permission.getGroupName();
				args[1] = permission.getName();
				args[2] = 2;
				listResult = this.queryForList("select * from t_permission where group_name = ? and show_type = 1 and name = ? and length(code) = ?", args);
				return null != listResult && listResult.size() > 0 ;	
			}
			else
			{
				Object [] args = new Object[3];
				args[0] = permission.getGroupName();
				args[1] = permission.getName();
				args[2] = permission.getParentCode();
				listResult = this.queryForList("select * from t_permission where group_name = ? and show_type = 1 and name = ? and parent_code = ? ", args);
				return null != listResult && listResult.size() > 0 ;
			}
			
		}
		else
		{
			Object [] args = new Object[4];
			args[0] = permission.getGroupName();
			args[1] = permission.getShowType();
			args[2] = permission.getName();
			args[3] = permission.getParentCode();
			listResult = this.queryForList("select * from t_permission where group_name = ? and show_type = ? and name = ? and parent_code = ? ", args);
			return null != listResult && listResult.size() > 0 ;
		}
		
	}
	/**
	 * add by zhoufb
	 */
	@Override
	public boolean isExitsUrl(Permission permission) 
	{
		List<Map<String,Object>> listResult = null;
		Object [] args = new Object[3];
		args[0] = permission.getGroupName();
		args[1]=permission.getUri();
		args[2]=permission.getParentCode();
		//禁用的要不要考虑？？
		listResult = this.queryForList("select * from t_permission where group_name = ? and api_uri = ? and parent_code = ? ", args);
		return null != listResult && listResult.size() > 0 ;
	}
	@Override
	public boolean isExitsUpdateName(Permission permission) 
	{
		List<Map<String,Object>> listResult = null;
		//显示类型(1菜单,2界面按钮,3列表行按钮)
		if(1 == permission.getShowType())
		{
			//判断是否是添加一级菜单 （添加一级菜单不会传递该值）
			if(StringUtils.isEmpty(permission.getParentCode()))
			{
				Object [] args = new Object[4];
				args[0] = permission.getGroupName();
				args[1] = permission.getName();
				args[2] = 2;
				args[3] = permission.getCode();
				listResult = this.queryForList("select * from t_permission where group_name = ? and show_type = 1 and name = ? and length(code) = ? and code != ?", args);
				return null != listResult && listResult.size() > 0;	
			}
			else
			{
				Object [] args = new Object[4];
				args[0] = permission.getGroupName();
				args[1] = permission.getName();
				args[2] = permission.getParentCode();
				args[3] = permission.getCode();
				listResult = this.queryForList("select * from t_permission where group_name = ? and show_type = 1 and name = ? and parent_code = ? and code != ?", args);
				return null != listResult && listResult.size() > 0;
			}
			
		}
		else
		{
			Object [] args = new Object[5];
			args[0] = permission.getGroupName();
			args[1] = permission.getShowType();
			args[2] = permission.getName();
			args[3] = permission.getParentCode();
			args[4] = permission.getCode();
			listResult = this.queryForList("select * from t_permission where group_name = ? and show_type = ? and name = ? and parent_code = ? and code != ?", args);
			return null != listResult && listResult.size() > 0;
		}
/*		List<Map<String,Object>> listResult = null;
		if(1 == permission.getShowType())
		{
			Object [] args = new Object[3];
			args[0] = permission.getGroupName();
			args[1] = permission.getName();
			args[2] = permission.getCode();
			listResult = this.queryForList("select * from t_permission where group_name = ? and show_type = 1 and name = ? and code != ?", args);
			return null != listResult && listResult.size() > 0 ;
		}
		else
		{
			Object [] args = new Object[3];
			args[0] = permission.getGroupName();
			args[1] = permission.getName();
			args[2] = permission.getParentCode();
			listResult = this.queryForList("select * from t_permission where group_name = ? and show_type != 1 and name = ? and parent_code = ?", args);
			return null != listResult && listResult.size() > 1 ;
		}*/
	}

	@Override
	public String getMaxCode(int codeLen,String parentcode) 
	{
		List<Map<String,Object>> listResult = null;
		if(!StringUtils.isEmpty(parentcode))
		{
			Object [] args = new Object[2];
			args[0] = codeLen;
			args[1] = parentcode+"%";
			listResult = this.queryForList("select max(code) as code  from t_permission where length(code) = ?  AND CODE LIKE ?", args);	
		}
		else
		{
			Object [] args = new Object[1];
			args[0] = codeLen;
			listResult = this.queryForList("select max(code) as code  from t_permission where length(code) = ?", args);	
		}
		
		if(null != listResult && listResult.size() > 0 )
		{
			if(null == listResult.get(0).get("code"))
			{
				return null;
			}
			return listResult.get(0).get("code").toString();
		}
		return null;
	}

	@Override
	public boolean isExitsChildMenu(Integer permissionId,String code) 
	{
		Object [] args = new Object[1];
		args[0] = code + "%";
		//查询是否还有子菜单
		List<Map<String,Object>> resultList = this.queryForList("select * from t_permission where code like ?", args);
		
		//查询是否有账号引用此账号组(除管理员账号)
		List<Map<String,Object>> roleList =  this.queryForList(" SELECT * FROM t_role_permission WHERE permission_id = ? and (role_id != 2 and role_id != 1)", permissionId);
		return (null != resultList && resultList.size() > 1) || (null != roleList && roleList.size() > 0);
	}

	@Override
	public void enable(Permission permission) 
	{
		Object [] args = new Object[2];
		args[0] = permission.getEnabled();
		args[1] = permission.getPermissionId();
		this.executeUpdate("update t_permission set enabled = ? where permission_id = ?", args);
		/*if(1 == permission.getEnabled())
		{
			this.executeUpdate("update t_permission set enabled = 1 where permission_id = ?", permission.getPermissionId());
		}
		else
		{
			String code = permission.getParentCode();
			if(StringUtils.isEmpty(code))
			{
				code = permission.getCode();
			}
			code = code + "%";
			this.executeUpdate("update t_permission set enabled = 0 where code like ?", code);
		}*/
		
	}
	
}
